﻿using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;
namespace DAO
{
    public class LOAIMONDAO
    {
        ///CỦA AI THÌ TỰ ĐÓNG KHUNG VÀO NHA. TRÁNH NGƯỜI KHÁC XÓA NHẦM CODE
        /////////////////////CỦA LINH//////////////////
        public List<LOAIMONDTO> LayDSLoai(BANDTO BDTO, bool loai)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            //Lấy danh sách loại món chưa gọi
            SqlCommand command = new SqlCommand("LINH25_DSLOAIMONCHUAGOI", conn);
            command.CommandType = System.Data.CommandType.StoredProcedure;

            command.Parameters.Add("@MaBan", System.Data.SqlDbType.Int);
            command.Parameters["@MaBan"].Value = BDTO.MaBan;

            command.Parameters.Add("@Loai", System.Data.SqlDbType.Bit);
            command.Parameters["@Loai"].Value = loai;

            SqlDataReader reader = command.ExecuteReader();
            List<LOAIMONDTO> ds = new List<LOAIMONDTO>();
            while (reader.Read())
            {
                LOAIMONDTO LDTO = new LOAIMONDTO();
                LDTO.MaLoai = reader.GetInt32(0);
                LDTO.TenLoai = reader.GetString(1);
                LDTO.TinhTrang = false;//Loại món chưa gọi
                ds.Add(LDTO);
            }
            //Lấy danh sách loại món đã gọi
            reader.Close();
            command = new SqlCommand("LINH24_DSLOAIMONGOIROI", conn);
            command.CommandType = System.Data.CommandType.StoredProcedure;

            command.Parameters.Add("@MaBan", System.Data.SqlDbType.Int);
            command.Parameters["@MaBan"].Value = BDTO.MaBan;

            command.Parameters.Add("@Loai", System.Data.SqlDbType.Bit);
            command.Parameters["@Loai"].Value = loai;
            
            reader = command.ExecuteReader();
            while (reader.Read())
            {
                LOAIMONDTO LDTO = new LOAIMONDTO();
                LDTO.MaLoai = reader.GetInt32(0);
                LDTO.TenLoai = reader.GetString(1);
                LDTO.TinhTrang = true;//Loại món đã gọi
                ds.Add(LDTO);
            }
            conn.Close();
            return ds;
        }
        public List<LOAIMONDTO> DSLoai(DateTime bd, DateTime kt, bool loai)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand command = new SqlCommand("LINH38_LOAIMONDABAN", conn);
            command.CommandType = System.Data.CommandType.StoredProcedure;

            command.Parameters.Add("@TGBatDau", System.Data.SqlDbType.DateTime);
            command.Parameters["@TGBatDau"].Value = bd;

            command.Parameters.Add("@TGKetThuc", System.Data.SqlDbType.DateTime);
            command.Parameters["@TGKetThuc"].Value = kt;

            command.Parameters.Add("@Loai", System.Data.SqlDbType.Bit);
            command.Parameters["@Loai"].Value = loai;

            SqlDataReader reader = command.ExecuteReader();
            List<LOAIMONDTO> ds = new List<LOAIMONDTO>();
            while (reader.Read())
            {
                LOAIMONDTO LDTO = new LOAIMONDTO();
                LDTO.MaLoai = reader.GetInt32(0);
                LDTO.TenLoai = reader.GetString(1);
                ds.Add(LDTO);
            }
            conn.Close();
            return ds;
        }
        //////////////////////////////////////////////
        public int ThemLoaiMon(LOAIMONDTO lmDTO)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand("ThemLoaiMon", conn);
            cmd.Parameters.Add("@tenloai", System.Data.SqlDbType.Text).Value = lmDTO.TenLoai;
            cmd.Parameters.Add("@loai", System.Data.SqlDbType.Bit).Value = lmDTO.Loai;
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            if (kq > 0)
            {
                return 1;
            }
            else { return 0; }
        }
        public List<LOAIMONDTO> LayDSLoaiMon()
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand("LayDSLoaiMon", conn);
            SqlDataReader reader = cmd.ExecuteReader();
            List<LOAIMONDTO> ds = new List<LOAIMONDTO>();
            while (reader.Read())
            {
                LOAIMONDTO lmDTO = new LOAIMONDTO();
                lmDTO.MaLoai = reader.GetInt32(0);
                lmDTO.TenLoai = reader.GetString(1);
                ds.Add(lmDTO);
            }
            conn.Close();
            return ds;
        }
        public List<LOAIMONDTO> LayDSLoaiMonFull()
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand("LayDSLoaiMonFull", conn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            SqlDataReader reader = cmd.ExecuteReader();
            List<LOAIMONDTO> ds = new List<LOAIMONDTO>();
            while (reader.Read())
            {
                LOAIMONDTO lmDTO = new LOAIMONDTO();
                lmDTO.TenLoai = reader.GetString(0);
                lmDTO.SoLuong = reader.GetInt32(1);
                ds.Add(lmDTO);
            }
            conn.Close();
            return ds;
        }
        public int XoaLoaiMon(LOAIMONDTO lmDTO)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand("XoaLoaiMon", conn);
            cmd.Parameters.Add("@tenloaimon", System.Data.SqlDbType.Text).Value = lmDTO.TenLoai;
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            if (kq > 0)
                return 1;
            else
                return 0;
        }
        public int SuaLoaiMon(LOAIMONDTO lmDTO)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand("SuaLoaiMon", conn);
            cmd.Parameters.Add("@tenloaicu", System.Data.SqlDbType.Text).Value = lmDTO.TenLoaiCu;
            cmd.Parameters.Add("@tenloaimoi", System.Data.SqlDbType.Text).Value = lmDTO.TenLoai;
            cmd.Parameters.Add("@loai", System.Data.SqlDbType.Bit).Value = lmDTO.Loai;
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            if (kq > 0)
                return 1;
            return 0;
        }
        public List<LOAIMONDTO> TimLoaiMon(LOAIMONDTO lmDTO)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand("TimLoaiMon", conn);
            cmd.Parameters.Add("@tenloaimon", System.Data.SqlDbType.NVarChar,50).Value = lmDTO.TenLoai;
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            SqlDataReader reader = cmd.ExecuteReader();
            List<LOAIMONDTO> ds = new List<LOAIMONDTO>();
            while (reader.Read())
            {
                lmDTO.TenLoai = reader.GetString(0);
                lmDTO.SoLuong = reader.GetInt32(1);
                ds.Add(lmDTO);
            }
            conn.Close();
            return ds;
        }
        public DataTable LayDSLoaiMonDaXoa()
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand("LayDSLoaiMonDaXoa", conn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            DataTable tb = new DataTable();
            SqlDataAdapter adap = new SqlDataAdapter(cmd);
            adap.Fill(tb);
            conn.Close();
            return tb;
        }
        public int PhucHoiLoaiMonDaXoa(LOAIMONDTO lmDTO)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand("PhucHoiLoaiMonDaXoa", conn);
            cmd.Parameters.Add("@tenloai", System.Data.SqlDbType.NVarChar, 50).Value = lmDTO.TenLoai;
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            if (kq > 0)
                return 1;
            return 0;
        }
        public int XoaThatSuLoaiMon(LOAIMONDTO lmDTO)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand("XoaThatSuLoaiMon", conn);
            cmd.Parameters.Add("@tenloai", System.Data.SqlDbType.NVarChar, 50).Value = lmDTO.TenLoai;
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            if (kq > 0)
                return 1;
            return 0;
        }
    }
}
